package com.haochang.chunkvod.controller.activity.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.hostess.InsteadChargeAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.listView.swipemenulistview.SwipeMenu;
import com.haochang.chunkvod.view.vod.listView.swipemenulistview.SwipeMenuCreator;
import com.haochang.chunkvod.view.vod.listView.swipemenulistview.SwipeMenuItem;
import com.haochang.chunkvod.view.vod.listView.swipemenulistview.SwipeMenuListView;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

/**
 * 代收列表页
 *
 * @author Yangjie
 *         className InsteadChargeListActivity
 *         created at  2016/8/23  18:08
 */
public class InsteadChargeListActivity extends BaseActivity {

    private TitleView titleView;
    private InsteadChargeAdapter adapter;
    private SwipeMenuListView listView;//可滑动删除item的listview
    private HostessData hostessData;
    private List<HostessInfo> listInsteadCharge = new ArrayList<HostessInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        requestInsteadChargingList(false);
    }

    @Override
    public void receiveParam() {
    }

    @Override
    public void initData() {
        hostessData = new HostessData(InsteadChargeListActivity.this);
    }

    @Override
    public void initViews() {
        setContentView(R.layout.activity_instead_charge_list);
        titleView = (TitleView) findViewById(R.id.titleView);
        ((BaseTextView) findViewById(R.id.tv_time)).setText("代付时间");
        titleView.setTitle(R.string.insteadCharge).setTitleOnClickListener(new TitleView.ITitleOnClickListener() {
            @Override
            public void onBackClick() {
                finish();
                animRightToLeft();
            }

            @Override
            public void onDoneClick() {
                List<String> list = new ArrayList<String>();
                list.add("回到首页");
                list.add("修改密码");
                list.add("退出登录");
                int location[] = new int[2];
                ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
                img.getLocationOnScreen(location);
                int x = location[0];
                final VodPopWindow pop = new VodPopWindow(InsteadChargeListActivity.this, list);
                pop.showPopupWindow(titleView, x);
                pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener() {
                    @Override
                    public void onSelectItem(int position) {
                        switch (position) {
                            case 0:
                                backToHome();
                                break;
                            case 1:
                                goToPasswordModify();
                                break;
                            case 2:
                                loginOut();
                                break;

                            default:
                                break;
                        }
                    }
                });
            }
        });

        listView = (SwipeMenuListView) findViewById(R.id.swipmenuListView);
        adapter = new InsteadChargeAdapter(InsteadChargeListActivity.this, R.layout.item_advances_received, listInsteadCharge);
        listView.setAdapter(adapter);
        initCreatorAndClick();
    }

    private void initCreatorAndClick() {
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "edit" item
                SwipeMenuItem editItem = new SwipeMenuItem(getApplicationContext());
                editItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
                editItem.setWidth(DipPxConversion.dip2px(InsteadChargeListActivity.this, 60));
                editItem.setTitle("编辑");
                editItem.setTitleSize(14);
                editItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(editItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                deleteItem.setWidth(DipPxConversion.dip2px(InsteadChargeListActivity.this, 60));
                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(14);
                deleteItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(deleteItem);
            }
        };
        listView.setMenuCreator(swipeMenuCreator);
        listView.setOpenInterpolator(new LinearInterpolator());
        listView.setCloseInterpolator(new LinearInterpolator());
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // edit
                        HostessInfo info = listInsteadCharge.get(position);
                        if (null!=info){

                        }
                        break;
                    case 1:
                        // delete
                        Log.e("yy", "listInsteadCharge:" + listInsteadCharge.size());
                        listInsteadCharge.remove(position);
                        adapter.remove(position);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        });
    }

    /**
     * 获取房间信息数据
     *
     * @param isLoading 网络请求时是否转圈
     */
    public void requestInsteadChargingList(boolean isLoading) {
        hostessData.requestReserveRoomStatusList(isLoading, new HostessData.ResultResponseList<HostessInfo>() {
            @Override
            public void onRequestSuccess(List<HostessInfo> list) {
                if (!CollectionUtils.isEmpty(list)) {
                    listInsteadCharge.clear();
                    listInsteadCharge.addAll(list);
                    adapter.replaceAll(listInsteadCharge);
                }
            }

            @Override
            public void onFail(int errno, String errMsg) {
                ToastUtils.showText(InsteadChargeListActivity.this, TextUtils.isEmpty(errMsg) ? "代付列表获取失败!" : errMsg);
            }
        });
    }

}
